var loggedIn = false;

$(document).ready(
		function() {
			FB.init("4172c77f1660f60e20693bc5cfdcc143", "xd_receiver.htm", {
				"ifUserConnected" :onConnected,
				"ifUserNotConnected" :onNotConnected
			});

			FB_RequireFeatures( [ "XFBML" ], function() {
				FB.Facebook.init("4172c77f1660f60e20693bc5cfdcc143",
						"xd_receiver.htm");
			});

			$("#imgDevil").click( function() {
				postChoice("devil");
			});

			$("#imgAngel").click( function() {
				postChoice("angel");
			});

			$("#imgDevil").hover( function() {
				$("#img_devil").hide();
				$("#img_devil_hover").show();
				this.style.cursor = 'pointer';
			}, function() {
				$("#img_devil").show();
				$("#img_devil_hover").hide();
			});

			$("#imgAngel").hover( function() {
				$("#img_angel").hide();
				$("#img_angel_hover").show();
				this.style.cursor = 'pointer';
			}, function() {
				$("#img_angel").show();
				$("#img_angel_hover").hide();
			});
		});

function onNotConnected() {
	$("#logout").hide();
}

function postChoice(choiceAngelOrDevil) {
	if(!loggedIn)
	{
		displayMessage("you must connect first");
		return;
	}
	
	var userFbId = getUserFbId();
	
	$.post("/data", {
		userid :userFbId,
		angelordevil :choiceAngelOrDevil
	}, function(data) {
		prepareInterface(eval("(" + data + ")"));
	});
}

$.fn.wait = function(time, type) {
    time = time || 2000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

function displayMessage(message) {
	$("#warningOrErrorMessage").html(message);
	$("#warningOrErrorMessage").slideDown("fast").wait().slideUp("fast");		
}

function prepareInterface(data) {
	
	if (data.loggedBefore && data.voted) {
		$("#NumOfAngel").text(data.numOfAngel);
		$("#NumOfDevil").text(data.numOfDevil);
		
		disableVotingAndPickResult(data.isAngel);
	}
};

function disableVotingAndPickResult(isAngel) {	
	$("#imgDevil").unbind();
	$("#imgAngel").unbind();
	
	if(isAngel) {
		$("#img_devil").show();
		$("#img_devil_hover").hide();
		$("#img_angel").hide();
		$("#img_angel_hover").show();
		document.getElementById("img_angel_hover").style.cursor = '';
	} else {
		$("#img_devil").hide();
		$("#img_devil_hover").show();
		$("#img_angel").show();
		$("#img_angel_hover").hide();
		document.getElementById("img_devil_hover").style.cursor = '';
	}
}

function onConnected() {
	$("#logout").show();	
	loggedIn = true;	
	getUIDataAndPrepareInterface();
};

function getUIDataAndPrepareInterface() {
	var userFbId = getUserFbId();

	$("#name").html(
			"<fb:name uid='" + userFbId
					+ "' useyou='false' linked='false'></fb:name>");
	$("#profilePic")
			.html(
					"<fb:profile-pic uid='loggedinuser' facebook-logo='true' linked='false' size='s'></fb:profile-pic>");
	$("#fbLoginButton").hide();
	$("#loggedInLeftPanel").show();

	$.getJSON("/data?userid=" + userFbId, function(data) {
		prepareInterface(data);
	});

	FB.XFBML.Host.parseDomTree();
}

function getUserFbId() {
	var loggedInUserId = null;

	FB_RequireFeatures( [ "Api" ], function() {
		var api = FB.Facebook.apiClient;
		api.requireLogin( function(exception) {
			loggedInUserId = api.get_session().uid;
		});
	});

	return loggedInUserId;
}


